package co.fitstart.admin.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Page;
import com.demo2do.core.utils.CollectionUtils;

import co.fitstart.admin.dao.CoachDao;
import co.fitstart.admin.dao.PrincipalDao;
import co.fitstart.admin.service.CoachService;
import co.fitstart.entity.coach.CoachSettlement;
import co.fitstart.entity.coach.CoachStudentRelation;
import co.fitstart.entity.coach.PrincipalReportRelation;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.principal.SpecialTags;
import co.fitstart.entity.site.Site;

/**
 * 
 * @author lidan
 *
 */
@Service("coachService")
public class CoachServiceImpl implements CoachService{
    
    @Dynamic
    private CoachDao coachDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Dynamic
    private PrincipalDao principalDao;

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CoachService#listCoachSettlement(co.fitstart.entity.principal.Principal)
     */
    public List<CoachSettlement> listCoachSettlement(Principal principal) {
        return coachDao.listCoachSettlement(principal);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CoachService#listCoachStudentRelation(co.fitstart.entity.principal.Principal, com.demo2do.core.support.Page)
     */
    public List<CoachStudentRelation> listCoachStudentRelation(Principal principal, Page page) {
        List<CoachStudentRelation> result = coachDao.listCoachStudentRelation(principal, page.getBeginIndex(), page.getMaxResultRecords());
        
        page.calculate(result.size());
        
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }
    
    /* (non-Javadoc)
     * @see co.fitstart.admin.service.system.CoachService#loadSettlement(java.lang.Long)
     */
    public CoachSettlement loadSettlement(Long id) {
        return genericDaoSupport.load(CoachSettlement.class, id);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.system.CoachService#editSettlement(co.fitstart.common.entity.coach.CoachSettlement)
     */
    public void editSettlement(CoachSettlement settlement) {
        genericDaoSupport.update(settlement);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CoachService#getCoachSite(co.fitstart.entity.principal.Principal)
     */
    public Site getCoachSite(Principal coach) {
        return coachDao.getCoachSite(coach);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CoachService#listCoachSite(co.fitstart.entity.principal.Principal, java.util.Date)
     */
    public List<Site> listCoachSite(Principal coach, Date day) {
        if(day == null) {
            return coachDao.listCoachSite(coach);
        } else {
            return coachDao.listCoachSite(coach, "%" + getDayOfWeek(day) + ",%");
        }
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CoachService#load(java.lang.Long)
     */
    public Principal load(Long coachId) {
        return genericDaoSupport.searchForObject("FROM Principal WHERE authority='ROLE_COACH' AND disabled=false AND id = :coachId", "coachId", coachId, Principal.class);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CoachService#listCoach(co.fitstart.entity.site.Site)
     */
    @SuppressWarnings("unchecked")
    public List<Principal> listCoach(Site site) {
        if (site == null) {
            List<PrincipalReportRelation> relations = coachDao.listCoaches();
            return CollectionUtils.extractPropertyList(relations, "coach");
        } else {
            List<PrincipalReportRelation> relations = coachDao.listCoaches(site);
            return CollectionUtils.extractPropertyList(relations, "coach");
        }
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CoachService#listCoach(co.fitstart.entity.site.Site, java.lang.String)
     */
    @SuppressWarnings("unchecked")
    public List<Principal> listCoach(Site site, SpecialTags tag) {
    	
    	if(tag != null) {
    		if (site == null) {
                List<PrincipalReportRelation> relations = coachDao.listCoaches("%"+tag.getCode()+"%");
                return CollectionUtils.extractPropertyList(relations, "coach");
            } else {
                List<PrincipalReportRelation> relations = coachDao.listCoaches(site, "%"+tag.getCode()+"%");
                return CollectionUtils.extractPropertyList(relations, "coach");
            }
    	} else {
    		return this.listCoach(site);
    	}
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CoachService#listCoach(java.util.List)
     */
    @SuppressWarnings("unchecked")
    public List<Principal> listCoach(List<Site> sites) {
        List<PrincipalReportRelation> relations = coachDao.listCoaches(sites);
        return CollectionUtils.extractPropertyList(relations, "coach");
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CoachService#editAppointDays(co.fitstart.entity.principal.Principal, co.fitstart.entity.principal.Principal, java.util.List)
     */
    public void editAppointDays(Principal coach, Principal manager, List<Integer> appointDays) {
        PrincipalReportRelation relation = principalDao.getRelation(coach, manager);
        
        if(relation != null) {
            if(appointDays == null) {
                genericDaoSupport.update(relation.editAppointDays(null));
            } else {
                List<Integer> days = new ArrayList<>();
                
                for(Integer appointDay : appointDays) {
                    if(appointDay >= 1 && appointDay <= 7) {
                        days.add(appointDay);
                    }
                }
                
                if(days.isEmpty()) {
                    throw new BusinessException("指定日期异常");
                } else {
                    genericDaoSupport.update(relation.editAppointDays(days));
                }
            }
        } else {
            throw new BusinessException("教练与店长不匹配");
        }
    }
    
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CoachService#editAppointHours(co.fitstart.entity.principal.Principal, co.fitstart.entity.principal.Principal, java.util.List)
     */
	public void editAppointHours(Principal coach, Principal manager, List<String> appointHours) {
		
		 PrincipalReportRelation relation = principalDao.getRelation(coach, manager);
	        
	        if(relation != null) {
	            if(appointHours == null) {
	                genericDaoSupport.update(relation.editAppointHours(null));
	            } else {
	                List<String> hours = new ArrayList<>();
	                
	               
	                
	                for(String appointHour : appointHours) {                	
	                	
		                hours.add(appointHour);
	                	
	                }
	                
	                if(hours.isEmpty()) {
	                    throw new BusinessException("指定日期异常");
	                } else {
	                    genericDaoSupport.update(relation.editAppointHours(hours));
	                }
	            }
	        } else {
	            throw new BusinessException("教练与店长不匹配");
	        }
	}

	/**
     * 
     * @param day
     * @return
     */
    private int getDayOfWeek(Date day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        
        if(dayOfWeek == 0) {
            return 7;
        } else {
            return dayOfWeek -1;
        }
    }
}
